package converter

import converter.util.MultiProjectChecker;

class SettingsProducer {

	private List fetcModuleList(File pom, String parentModule) {
		println "Input file $pom.absolutePath"
		assert pom.exists() : "The file [$pom.absolutePath] does not exist!"
		List modules = []
		MultiProjectChecker mpc = new MultiProjectChecker(pom)
		if(!mpc.isMultiProject())  {
			return modules
		}
		Map map =mpc.getModulesFileMap()
		map.each{module, file ->
			    if(!parentModule) {
			    	modules <<  "\"$module\""
				}else {
					modules <<  "\"$parentModule:$module\""
				}
				List subModules = fetcModuleList(file, module)
				if(subModules) {
					modules.addAll(subModules)
				}
		}
		return modules
	}

	void produceSettings(File pom) {
		MultiProjectChecker mpc = new MultiProjectChecker(pom)
		if(!mpc.isMultiProject()) {
			println "Not a multiproject, settings not generated!"
			return
		}
		List modules = fetcModuleList(pom, null)
		modules.each {
			println "$it"
		}
	}
}

